﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Weight.Base;

namespace Weight.Controls
{
    public class LYFFormTitleBar : Panel
    {
        public LYFFormTitleBar()
        {
            this.SetStyle(
                 //ControlStyles.UserPaint |//使用自定义的绘制方式
                 ControlStyles.ResizeRedraw//当控件大小发生变化时就重新绘制
                | ControlStyles.SupportsTransparentBackColor//则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度
                | ControlStyles.AllPaintingInWmPaint//则控件忽略窗口消息 WM_ERASEBKGND 以减少闪烁
                | ControlStyles.OptimizedDoubleBuffer//则控件将首先绘制到缓冲区而不是直接绘制到屏幕，这可以减少闪烁
                , true);
            this.Size = new System.Drawing.Size(this.Width, 45);
            this.Dock = DockStyle.Top;
            this.BackColor = System.Drawing.ColorTranslator.FromHtml("#00a381");
            _titleImage = Properties.Resources.logo;
            _titleText = this.Name;

            RectanglePointInit();


        }
        #region 属性
        private Image _titleImage;
        [System.ComponentModel.Browsable(true)]
        public Image TitleImage { get => _titleImage; set { _titleImage = value; this.Invalidate(); } }

        private string _titleText = "";
        public string TitleText
        {
            get { return _titleText; }
            set
            {
                _titleText = value;
                this.Invalidate();
            }
        }

        private string _mainTitleText = "";
        public string MainTitleText
        {
            get { return _mainTitleText; }
            set
            {
                _mainTitleText = value;
                this.Invalidate();
            }
        }

        private bool _doubleLeftMaximized = true;
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(bool), "true")]
        public bool DoubleLeftMaximized { get => _doubleLeftMaximized; set => _doubleLeftMaximized = value; }

        private bool _doubleRightClose = true;
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.DefaultValue(typeof(bool), "true")]
        public bool DoubleRightClose { get => _doubleRightClose; set => _doubleRightClose = value; }

        #endregion

        #region 自定义
        //方式1
        private Rectangle closeRect;
        private Rectangle closeBackRect;
        //方式2
        private Point closeP1;
        private Point closeP2;
        private Point closeP3;
        private Point closeP4;
        //方式1
        private Rectangle minRect;
        private Rectangle minBackRect;
        //方式2
        private Point minP1;
        private Point minP2;

        private MouseStatus mouseClose = default(MouseStatus);
        private MouseStatus mouseMin = default(MouseStatus);
        #endregion

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
            if (TitleImage != null)
                e.Graphics.DrawImage(TitleImage, new Rectangle((int)(this.Height * 0.2),
                                                               (int)(this.Height * 0.2),
                                                               (int)(this.Height * 0.6),
                                                               (int)(this.Height * 0.6)));
            if (TitleText != null)
                e.Graphics.DrawString(TitleText,
                    this.Font,
                    new SolidBrush(this.ForeColor),
                    new Point(this.Height, (this.Height - this.Font.Height) / 2));

            var mainTitleFont = new Font("宋体", 14, FontStyle.Bold);
            Size textSize = TextRenderer.MeasureText(_mainTitleText, mainTitleFont);
            if (TitleText != null)
                e.Graphics.DrawString(_mainTitleText,
                    new Font("宋体",14,FontStyle.Bold),
                    new SolidBrush(this.ForeColor),
                    new Point((this.Width- textSize.Width)/ 2+30, (this.Height - this.Font.Height) / 2));
            //e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, Color.White)), closeBackRect);
            //e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, Color.White)), minBackRect);
            Pen penEnter = new Pen(Color.White, 1f);
            Pen penNormal = new Pen(Color.FromArgb(255, Color.FromArgb(0xd8d8d8)), 1f);
            if (mouseClose == MouseStatus.MouseEnter)
            {
                //e.Graphics.DrawImage(Properties.Resources.Close_Enter, closeRect);
                e.Graphics.DrawLine(penEnter, closeP1, closeP2);
                e.Graphics.DrawLine(penEnter, closeP3, closeP4);
            }
            else
            {
                //e.Graphics.DrawImage(Properties.Resources.Close_Normal, closeRect);
                e.Graphics.DrawLine(penNormal, closeP1, closeP2);
                e.Graphics.DrawLine(penNormal, closeP3, closeP4);
            }
            if (mouseMin == MouseStatus.MouseEnter)
            {
                //e.Graphics.DrawImage(Properties.Resources.Min_Enter, minRect);
                e.Graphics.DrawLine(penEnter, minP1, minP2);
            }
            else
            {
                //e.Graphics.DrawImage(Properties.Resources.Min_Normal, minRect);
                e.Graphics.DrawLine(penNormal, minP1, minP2);
            }
            base.OnPaint(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            var pt = new Point(e.X, e.Y);
            Form form = BaseControls.Instance.ParentForm(this);
            if (form == null) return;

            if (closeBackRect.Contains(pt))
                form.Close();
            else if (minBackRect.Contains(pt))
                BaseControls.Instance.SetValueReflection(form, FormWindowState.Minimized, "WindowState");
            else
                BaseControls.Instance.Move(form, e, _doubleLeftMaximized);
            base.OnMouseDown(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            var pt = new Point(e.X, e.Y);
            if (closeBackRect.Contains(pt))
                mouseClose = MouseStatus.MouseEnter;
            else
                mouseClose = MouseStatus.MouseLeave;

            if (minBackRect.Contains(pt))
                mouseMin = MouseStatus.MouseEnter;
            else
                mouseMin = MouseStatus.MouseLeave;

            if (MouseStatus.MouseEnter == mouseMin || MouseStatus.MouseEnter == mouseClose)
                this.Cursor = Cursors.Hand;
            else
                this.Cursor = Cursors.Default;

            this.Invalidate();
            base.OnMove(e);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            if (this.Height < 45)
                this.Height = 45;

            RectanglePointInit();
            base.OnSizeChanged(e);
        }

        public void SetSkin(Color color)
        {
            this.BackColor = color;
        }

        private void RectanglePointInit()
        {
            int buttonWidth = 11;
            int buttonRectWidth = buttonWidth + 4;
            closeRect = new Rectangle(this.Width - 30, (this.Height - buttonWidth) / 2, buttonWidth, buttonWidth);
            closeBackRect = new Rectangle(this.Width - 30 - 2, (this.Height - buttonRectWidth) / 2, buttonRectWidth, buttonRectWidth);
            closeP1 = new Point(closeRect.X, closeRect.Y);
            closeP2 = new Point(closeRect.X + closeRect.Width, closeRect.Y + closeRect.Height);
            closeP3 = new Point(closeRect.X, closeRect.Y + closeRect.Height);
            closeP4 = new Point(closeRect.X + closeRect.Width, closeRect.Y);

            minRect = new Rectangle(this.Width - 60, (this.Height - buttonWidth) / 2, buttonWidth, buttonWidth);
            minBackRect = new Rectangle(this.Width - 60 - 2, (this.Height - buttonRectWidth) / 2, buttonRectWidth, buttonRectWidth);
            minP1 = new Point(minRect.X, minRect.Y + (minRect.Height) / 2);
            minP2 = new Point(minRect.X + minRect.Width, minRect.Y + (minRect.Height) / 2);
        }

        private enum MouseStatus
        {
            MouseNone,
            MouseEnter,
            MouseLeave,
        }
    }
}
